<!DOCTYPE html>
<html>
<head>
	{include file='top.tpl'}
</head>
<body class="skin-blue">
<div class="wrapper">
	{include file="bread_crumbs.tpl"}
	<!--引入面包屑-->
	<section class="web-setting-wrap">
		<div class="row">
			<div class="col-md-12">
				<div class="box">
	                <div class="box-header with-border clearfix">
	                	<h3 class="box-title pull-left"><a href="javascript:void(0);" class="btn btn-primary btn-sm" id='js_addRole'><i class="fa fa-plus mr5"></i>新增角色</a></h3>
	                	<div class="box-form pull-right form-group-sm">
	                		<form action="/system/role/index" method="get">
	                			<p class="pull-right"><button type="submit" class="btn btn-primary btn-sm btn-block">搜索</button></p>
	                			<p class="pull-right"><input type="text" class="form-control" name='keyword' value='{$params.keyword}'/></p>
	                			<p class="pull-right">
	                				<select class="form-control" name='search_type'>
	                					<option value="role_name">角色名称</option>
	                				</select>
	                			</p>
	                		</form>
	                	</div>
	                </div><!-- /.box-header -->
	                <div class="box-body">
	                  <table class="table table-bordered table-striped table-hover">
                        <thead>
                            <tr>
                            <th width="200">操作</th>
                            <th width="100">序号</th>
                            <th width="200">角色名称</th>
                            <th>简单描述</th>
                            <th width="200">添加时间</th>
                            </tr>
                        </thead>
	                    {if count($datas)>0}
                        <tbody>
                            {foreach from=$datas key=key item=data}    	                    
    	                    <tr>
                                <td>
                                    <a href="javascript:void(0);" class="btn btn-primary btn-sm" data-toggle="tooltip" data-original-title="编辑角色" name='edit_role' role_id='{$data.role_id}'><i class="fa fa-pencil"></i></a>	                      	
                                    <a href="/system/role/auth?id={$data.role_id}" class="btn btn-primary btn-sm" data-toggle="tooltip" data-original-title="编辑角色权限" name='edit_role_right' role_id='{$data.role_id}'><i class="fa fa-key"></i></a>
                                    <a href="/system/user/index?role_id={$data.role_id}" class="btn btn-success btn-sm"data-toggle="tooltip" data-original-title="编辑关联用户" name='edit_role_relation' role_id='{$data.role_id}'><i class="fa fa-user"></i></a>
                                    <a href="javascript:void(0);" class="btn btn-danger btn-sm" data-toggle="tooltip" data-original-title="删除" name='del_role' role_id='{$data.role_id}' role_name='{$data.role_name}'><i class="fa fa-remove"></i></a>
                                </td>
                                <td>{$data.role_id}</td>
                                <td>{$data.role_name}</td>
                                <td>{$data.description}</td>
                                <td>{$data.create_time}</td>
    	                    </tr>
    	                    {/foreach}
                        </tbody>
                        {/if}	                    
	              	</table>
	                </div>
	                <!-- .box-body -->
	                <div class="box-footer text-center clearfix">
                        
                    </div>
	              </div>
			</div>
		</div>
	</section>
</div>

<div style='display:none;width:400px;padding-top:10px;padding-left:10px;' id="js_RemarkPop">
    <div class="form-group">
        <label>角色名称：</label>
        <input type="text" name="name" id="name" class="form-control">
    </div>
    <div class="form-group">
        <label>简要描述：</label>
        <input type="text" name="description" id="description" class="form-control">
    </div>
    <div class=" text-center pt10">
        <input type="hidden" name="role_id_edit" id="role_id_edit"/> 
        <button type="submit" class="btn btn-primary fb pr20 pl20" onclick="return role_save()">保存</button>
    </div>
</div>

{include file='foot.tpl'}
{literal}
<script>
//添加角色
$("#js_addRole").on("click", function() {
    $("#name").val('');
    $("#description").val('');
    $("#role_id_edit").val('');
    layer.open({
        type: 1,
        title:'新增角色',
        skin: 'layui-layer-lan',
        area: ['420px', '240px'], //宽高
        content: $("#js_RemarkPop")  //也可以 $("#div")
    });
});

//编辑角色
$("a[name='edit_role']").on("click",function(){
    var role_id = $(this).attr('role_id');
    $.ajax({
        type:"POST",
        url:"/system/role/getrole",
        data:{'id':role_id},
        success:function(res){
            var obj = eval('('+res+')');
            if (obj.status == 0) {
                layer.alert(obj.message);
                return false;
            };
            $("#name").val(obj.role_name);
            $("#description").val(obj.description);
            $("#role_id_edit").val(obj.role_id);
            layer.open({
                type: 1,
                title:'编辑角色',
                skin: 'layui-layer-lan',
                area: ['420px', '240px'], //宽高
                content: $("#js_RemarkPop")  //也可以 $("#div")
            });
        }
    });
});

//删除菜单
$("a[name='del_role']").on('click',function(){
	del_role($(this));
});

function del_role($that){
	var role_id = $that.attr('role_id');
    var role_name = $that.attr('role_name');
    layer.confirm('您确定删除 '+role_name+' 吗？', function(){
    //删除成功
        $.ajax({
            type:"POST",
            url:"/system/role/delrole",
            data:{'id':role_id,'role':role_name},
            success:function(res){
                var obj = eval('('+res+')');
                if(obj.opr == '1'){
                    layer.alert(obj.content);
                    location.reload();
                }else if(obj.opr == '0'){
                    layer.alert(obj.content);
                } else {
                    layer.alert(obj.message);
                }
            }
        }); 
    });
}
function role_save(){
    var role_id_edit = $("#role_id_edit").val();
    var name = $.trim($("#name").val());
    var description = $.trim($("#description").val());
    if(name == ''){
        layer.alert('角色名称不能为空!',8);
        return false;
    }    
    $.ajax({
        type:"POST",
        url:"/system/role/replacerole",
        data:{'role_id_edit':role_id_edit,'name':name,'description':description},
        dataType: 'json',
        success:function(res){
            if(res.opr == '1'){
                layer.alert(res.content,8);
                location.reload();
            }else if(res.opr == '0'){
                layer.alert(res.content,8);
            } else {
                layer.alert(res.message);
            }
        }
    });
}
</script>
{/literal}
</body>
</html>
